package org.ccay.security.exception;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.ccay.core.exception.IEntityRelated;
import org.ccay.core.exception.IHttpCodeProvided;
import org.ccay.core.util.HttpUtil;
import org.ccay.core.util.JsonUtil;
import org.ccay.security.entity.Permission;
import org.ccay.security.entity.User;

/**
 * 授权异常
 * 
 * Created：2011-9-20
 * @author chaowangbang
 * @version:
 */
public class AuthorizationException extends SecuritySystemException implements IHttpCodeProvided,  IEntityRelated{
 
	private static final long serialVersionUID = -8038078683864952814L;
	
	private String entity;

	/**
	 * 授权异常构造函数。
	 * @param errorCode
	 */
	public AuthorizationException(String errorCode) {
		super(errorCode);
	}
	
	/**
	 * 授权异常构造函数。
	 * @param errorCode
	 * @param args
	 */
	public AuthorizationException(String errorCode, Object... args) {
		super(errorCode, args);
	}
	

	/**
	 * 创建带有关联实体的异常	
	 * @param principal
	 * @param permission
	 */
	public AuthorizationException(String userString, String permissionString) {
		this();
		Map<String,Object> entity = new HashMap<String,Object>();
		entity.put("user", userString);
		entity.put("permission", permissionString);
		try {
			this.setEntity(JsonUtil.objectToJson(entity));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 授权异常构造函数。
	 */
	public AuthorizationException() {
		//您无权执行该操作
		super("ccay.security.00010001");
	}

	/* (non-Javadoc)
	 * @see www.ccay.org.ccay.core.exception.IHttpCodeProvided#getHttpCode()
	 */
	public int getHttpCode() {
		return HttpUtil.NO_PERMISSION;
	}
	
	/** 
	 * 设置关联实体
	 * 
	 * @author chaowangbang
	 * @since 2012-11-8
	 * @param entity
	 */
	public void setEntity(String entity){
		this.entity  = entity;
	}

	/* (non-Javadoc)
	 * @see www.ccay.org.ccay.core.exception.IEntityRelated#getEntity()
	 */
	public String getEntity() {
		return entity;
	}

}
